//
//  CascadeListFrameView.m
//  etionRichText
//
//  Created by wangjian on 11/15/13.
//
//

#import "CascadeListFrameView.h"

#define CLFV_MARGIN 10
#define BAR_H 40
#define NAV_BTN_W 25
#define DURATION .3
#define NAV_TITLE_FONT [UIFont systemFontOfSize:16]

@implementation CCascadeNavView
{
    UIView *_contentView;
    NSMutableArray *_titles;
    UILabel *_titleLable;
    UILabel *_popTitleLable;
    UIImageView *_popImageView;
    UIButton *_popBtn;
    UIImageView *_closeImageView;
    UIButton *_closeBtn;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.layer.shadowOffset = CGSizeMake(0, .1);
        self.layer.shadowOpacity = .2;
        
        UIView *bgView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
        bgView.alpha = .95;
        bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        bgView.backgroundColor = [UIColor whiteColor];//[UIColor colorWithHex:0xe6e6e6];
        [self addSubview:bgView];
        
//        UIImageView *bgImageView = [[[UIImageView alloc] initWithFrame:self.bounds] autorelease];
//        bgImageView.image = [[UIImage imageNamed:@"nav_navbg"] resizeableCenterImage];
//        bgImageView.alpha = .95;
//        [self addSubview:bgImageView];
        
        _contentView = [[[UIView alloc] initWithFrame:CGRectMake(NAV_BTN_W, 0, self.width-2*NAV_BTN_W, self.height)] autorelease];
        _contentView.clipsToBounds = YES;
        [self addSubview:_contentView];
        
        _popImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav_back"]] autorelease];
        _popImageView.frame = CGRectMake(CLFV_MARGIN, (self.height-_popImageView.image.size.height)/2, _popImageView.image.size.width, _popImageView.image.size.height);
        _popImageView.alpha = 0.0;
        [self addSubview:_popImageView];
        
        _popBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_popBtn addTarget:self action:@selector(popBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _popBtn.frame = CGRectMake(0, 0, 100, self.height);
        _popBtn.hidden = YES;
        _popBtn.alpha = 0.0;
        [self addSubview:_popBtn];
        
        _closeImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map_cancel"]] autorelease];
        _closeImageView.frame = CGRectMake(self.width-CLFV_MARGIN-_closeImageView.image.size.width, (self.height-_closeImageView.image.size.height)/2, _closeImageView.image.size.width, _closeImageView.image.size.height);
        [self addSubview:_closeImageView];
        
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _closeBtn.frame = CGRectMake(self.width-100, 0, 100, self.height);
        [self addSubview:_closeBtn];

        _titles = [[NSMutableArray alloc] init];
        
        //search bar
        UIView *barContainView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
        barContainView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        barContainView.clipsToBounds = YES;
        [self insertSubview:barContainView aboveSubview:bgView];
        _searchBar = [[[XUISearchBar alloc] initWithFrame:CGRectMake(0, BAR_H, self.width, BAR_H) styleType:XUISearchBarStyleTypeDefault] autorelease];
        [_searchBar setBackgroundColor:[UIColor clearColor]];
//        if ([[UIDevice currentDevice].systemVersion intValue] < 7)
//        {
//            _searchBar.m_searchfield.borderStyle = UITextBorderStyleNone;
//        }
        
        [barContainView addSubview:_searchBar];
    }
    return self;
}

- (void)dealloc
{
    [_titles release];
    [super dealloc];
}

- (void)setSearchBarVisible:(BOOL)searchBarVisible animated:(BOOL)animated
{
    _searchBarVisible = searchBarVisible;
    if (animated == NO)
    {
        if (searchBarVisible == YES)
        {
            self.height = 2*BAR_H;
        }
        else
        {
            self.height = BAR_H;
            [self.searchBar.searchfield resignFirstResponder];
        }
    }
    else
    {
        [UIView animateWithDuration:.25 animations:^
        {
            if (searchBarVisible == YES)
            {
                self.height = 2*BAR_H;
            }
            else
            {
                [self.searchBar.searchfield resignFirstResponder];
                self.height = BAR_H;
            }
        }];
    }
}

- (UILabel *)lableWithType:(NSInteger)type text:(NSString *)text //type = 0 当前标题; = 1 上一层标题
{
    CGSize size = [text stringSizeWithFont:NAV_TITLE_FONT];
    UILabel *lable = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)] autorelease];
    lable.backgroundColor = [UIColor clearColor];
    lable.text = text;
    lable.font = NAV_TITLE_FONT;
    lable.textColor = type == 0 ? [UIColor blackColor] : [UIColor colorWithHex:0x4a99d3];
    lable.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [_contentView addSubview:lable];
    return lable;
}

- (CGRect)frameForTitleType:(NSInteger)type withTitle:(NSString *)title prevTitle:(NSString *)prevTitle
{
    CGRect frame = CGRectZero;
    CGSize titleSize = [title stringSizeWithFont:NAV_TITLE_FONT];
    CGSize prevTitleSize = [prevTitle stringSizeWithFont:NAV_TITLE_FONT];
    if (titleSize.width+prevTitleSize.width+CLFV_MARGIN > _contentView.width)
    {
        prevTitleSize.width = _contentView.width - CLFV_MARGIN - titleSize.width;
        if (prevTitleSize.width < _contentView.width/3)
        {
            prevTitleSize.width = _contentView.width/3;
            titleSize.width = _contentView.width - CLFV_MARGIN - prevTitleSize.width;
        }
    }
    
    if (type == 0)
    {
        frame = CGRectMake((_contentView.width-titleSize.width)/2, (_contentView.height-titleSize.height)/2, titleSize.width, titleSize.height);
        if (prevTitleSize.width+CLFV_MARGIN > (_contentView.width-titleSize.width)/2)
        {
            frame = CGRectMake(prevTitleSize.width+CLFV_MARGIN, frame.origin.y, frame.size.width, frame.size.height);
        }
    }
    else
    {
        frame = CGRectMake(0, (_contentView.height-prevTitleSize.height)/2, prevTitleSize.width, prevTitleSize.height);
    }
    
    return frame;
}

- (void)pushTitle:(NSString *)title animated:(BOOL)animated
{
    UILabel *curTitleLable = [self lableWithType:0 text:title];
    
    if (_titleLable == nil)
    {
        _titleLable = curTitleLable;
        _titleLable.frame = [self frameForTitleType:0 withTitle:title prevTitle:nil];
    }
    else if (_titles.count > 0)
    {
        if (animated == NO)
        {
            [_popTitleLable removeFromSuperview];
            _popTitleLable = [self lableWithType:1 text:_titleLable.text];
            _popTitleLable.frame = [self frameForTitleType:1 withTitle:title prevTitle:_popTitleLable.text];
            
            [_titleLable removeFromSuperview];
            _titleLable = curTitleLable;
            _titleLable.frame = [self frameForTitleType:0 withTitle:title prevTitle:_popTitleLable.text];
            
            if (_titles.count == 1)
            {
                _popBtn.hidden = NO;
                _popBtn.alpha = 1.0;
                _popImageView.alpha = 1.0;
            }
        }
        else
        {
            UILabel *prevTitleLable = _titleLable;
            _titleLable = curTitleLable;
            _titleLable.frame = [self frameForTitleType:0 withTitle:title prevTitle:prevTitleLable.text];
            _titleLable.alpha = 0.0;
            
            UILabel *prevPopTitleLable = _popTitleLable;
            _popTitleLable = [self lableWithType:1 text:prevTitleLable.text];
            _popTitleLable.frame = [self frameForTitleType:1 withTitle:title prevTitle:prevTitleLable.text];
            _popTitleLable.alpha = 0.0;
            
            CGFloat titleLableX = _titleLable.origin.x;
            _titleLable.frame = CGRectMake(_contentView.width, _titleLable.top, _titleLable.width, _titleLable.height);
            prevTitleLable.frame = CGRectMake(prevTitleLable.left, prevTitleLable.top, _popTitleLable.width, _popTitleLable.height);
            _popTitleLable.frame = prevTitleLable.frame;
            
            [UIView animateWithDuration:DURATION animations:^{
                _titleLable.frame = CGRectMake(titleLableX, _titleLable.top, _titleLable.width, _titleLable.height);
                _titleLable.alpha = 1.0;
                prevTitleLable.frame = CGRectMake(0, prevTitleLable.top, prevTitleLable.width, prevTitleLable.height);
                prevTitleLable.alpha = 0.0;
                _popTitleLable.frame = prevTitleLable.frame;
                _popTitleLable.alpha = 1.0;
                prevPopTitleLable.frame = CGRectMake(-prevPopTitleLable.width, prevPopTitleLable.top, prevPopTitleLable.width, prevPopTitleLable.height);
                prevPopTitleLable.alpha = 0.0;
                if (_titles.count == 1)
                {
                    _popBtn.hidden = NO;
                    _popBtn.alpha = 1.0;
                    _popImageView.alpha = 1.0;
                }
            } completion:^(BOOL finished) {
                [prevTitleLable removeFromSuperview];
                [prevPopTitleLable removeFromSuperview];
            }];
        }
        if (_popBtn.hidden == NO)
        {
            _popBtn.frame = CGRectMake(0, 0, _popTitleLable.right > 100 ? _popTitleLable.right : 100, self.height);
        }
    }
    [_titles addObject:title];
}

- (void)popTitleAnimated:(BOOL)animated
{
    if (_titles.count <= 1)
    {
        return;
    }
    
    NSString *popTitle = nil;
    if (_titles.count >= 3)
    {
        popTitle = [_titles objectAtIndex:_titles.count-3];
    }
    
    if (animated == NO)
    {
        [_titleLable removeFromSuperview];
        _titleLable = [self lableWithType:0 text:_popTitleLable.text];
        _titleLable.frame = [self frameForTitleType:0 withTitle:_popTitleLable.text prevTitle:popTitle];
        
        [_popTitleLable removeFromSuperview];
        if (popTitle.length > 0)
        {
            _popTitleLable = nil;
            _popBtn.hidden = YES;
            _popBtn.alpha = 0.0;
            _popImageView.alpha = 0.0;
        }
        else
        {
            _popTitleLable = [self lableWithType:1 text:popTitle];
            _popTitleLable.frame = [self frameForTitleType:1 withTitle:_titleLable.text prevTitle:popTitle];
        }
    }
    else
    {
        UILabel *prevTitleLable = _titleLable;
        _titleLable = [self lableWithType:0 text:_popTitleLable.text];
        _titleLable.frame = [self frameForTitleType:0 withTitle:_popTitleLable.text prevTitle:popTitle];
        _titleLable.alpha = 0.0;
        
        UILabel *prevPopTitleLable = _popTitleLable;
        if (popTitle.length > 0)
        {
            _popTitleLable = [self lableWithType:1 text:popTitle];
            _popTitleLable.frame = [self frameForTitleType:1 withTitle:prevPopTitleLable.text prevTitle:popTitle];
            _popTitleLable.alpha = 0.0;
        }
        else
        {
            _popTitleLable = nil;
        }
        
        
        CGFloat titleLableX = _titleLable.origin.x;
        _titleLable.frame = CGRectMake(0, _titleLable.top, _titleLable.width, _titleLable.height);
        _popTitleLable.frame = CGRectMake(-_popTitleLable.width, _popTitleLable.top, _popTitleLable.width, _popTitleLable.height);
        prevPopTitleLable.frame = _titleLable.frame;
        
        [UIView animateWithDuration:DURATION animations:^{
            _titleLable.frame = CGRectMake(titleLableX, _titleLable.top, _titleLable.width, _titleLable.height);
            _titleLable.alpha = 1.0;
            prevTitleLable.frame = CGRectMake(_contentView.width, prevTitleLable.top, prevTitleLable.width, prevTitleLable.height);
            prevTitleLable.alpha = 0.0;
            _popTitleLable.frame = CGRectOffset(_popTitleLable.frame, _popTitleLable.width, 0);
            _popTitleLable.alpha = 1.0;
            prevPopTitleLable.frame = _titleLable.frame;
            prevPopTitleLable.alpha = 0.0;
        } completion:^(BOOL finished) {
            [prevTitleLable removeFromSuperview];
            [prevPopTitleLable removeFromSuperview];
        }];
        if (_titles.count == 2)
        {
            [UIView animateWithDuration:DURATION animations:^{
                _popBtn.alpha = 0.0;
                _popImageView.alpha = 0.0;
            } completion:^(BOOL finished) {
                _popBtn.hidden = YES;
            }];
        }
    }
    
    [_titles removeLastObject];
}

- (void)popBtnClicked:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(cascadeNavView:willPopupTitleAtIndex:)])
    {
        if ([self.delegate cascadeNavView:self willPopupTitleAtIndex:_titles.count-1] == NO)
        {
            return;
        }
    }
    [self popTitleAnimated:YES];
    if ([self.delegate respondsToSelector:@selector(cascadeNavView:didPopupTitleAtIndex:)])
    {
        [self.delegate cascadeNavView:self didPopupTitleAtIndex:_titles.count];
    }
}

- (void)closeBtnClicked:(id)sender
{
    [self.delegate cascadeNavViewCloseBtnClicked:self];
//    static int testCnt = 0;
//    testCnt++;
//    [self pushTitle:[NSString stringWithFormat:@"test title string %d", testCnt] animated:YES];
}

@end

@interface CCascadeListFrameView() <CCascadeNavViewDelegate, CCascadeListViewDelegate, XUISearchBarDelegate>

//@property (nonatomic, retain) NSArray *arData;
@property (nonatomic, retain) CCascadeData *data;
@property (nonatomic, retain) NSMutableArray *dataStack;
@property (nonatomic, retain) NSMutableArray *arListViews;

@end

@implementation CCascadeListFrameView
{
    CCascadeNavView *_navView;
    UIView *_contentView;
    UIView *_completeView;
    ECascadeSelectorOperateType _operateType;
    CCascadeSelectorCellData *_curSelData;  //for single selector
}

- (void)dealloc
{
    [_dataStack release];
    [_arListViews release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame data:(CCascadeData *)data
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIImageView *bgImageView = [[[UIImageView alloc] initWithFrame:self.bounds] autorelease];
        bgImageView.image = [[UIImage imageNamed:@"gtv_group_bg"] resizeableCenterImage];
        [self addSubview:bgImageView];
        
        _contentView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
        _contentView.layer.masksToBounds = YES;
        _contentView.backgroundColor = [UIColor clearColor];
        _contentView.frame = CGRectMake(1, 1, self.width-2, self.height-2.5);
        _contentView.layer.cornerRadius = 2.5;
        [self addSubview:_contentView];
        
        _navView = [[[CCascadeNavView alloc] initWithFrame:CGRectMake(0, 0, _contentView.width, BAR_H)] autorelease];
        _navView.delegate = self;
        _navView.searchBar.delegate = self;
        _navView.searchBar.searchfield.returnKeyType = UIReturnKeyDone;
        [_contentView addSubview:_navView];
        
        _completeView = [[[UIView alloc] initWithFrame:CGRectMake(0, _contentView.height, _contentView.width, BAR_H)] autorelease];
        _completeView.layer.shadowOpacity = .2;
        _completeView.layer.shadowOffset = CGSizeMake(0, -0.1);
        [_contentView addSubview:_completeView];
        UIView *completeBgView = [[[UIView alloc] initWithFrame:_completeView.bounds] autorelease];
        completeBgView.backgroundColor = [UIColor whiteColor];//[UIColor colorWithHex:0xe6e6e6];
        completeBgView.alpha = .95;
        [_completeView addSubview:completeBgView];
        UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [completeBtn setTitle:@"确定" forState:UIControlStateNormal];
        completeBtn.frame = _completeView.bounds;
        [completeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        completeBtn.titleLabel.font = NAV_TITLE_FONT;
        [completeBtn addTarget:self action:@selector(completeBtnClecked:) forControlEvents:UIControlEventTouchUpInside];
        [_completeView addSubview:completeBtn];
        
        self.arListViews = [NSMutableArray array];
        self.dataStack = [NSMutableArray array];
        
        self.data = data;
        [self pushWithData:self.data.rootData animated:NO];
        if (data.operateType != ECascadeSelectorOperateTypeSingleSel)
        {
            _completeView.top = _contentView.height - _completeView.height;
        }
        else
        {
            [self.data enumerateObjectsUsingBlock:^(CCascadeSelectorCellData *data, NSUInteger level, NSUInteger index, BOOL *stop)
            {
                if (data.selected == YES)
                {
                    _curSelData = data;
                    *stop = YES;
                }
            }];
        }
        
        UISwipeGestureRecognizer *backSwipe = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backSwipeGestureTrigger:)] autorelease];
        backSwipe.direction = UISwipeGestureRecognizerDirectionRight;
        [self addGestureRecognizer:backSwipe];
    }
    return self;
}

- (void)backSwipeGestureTrigger:(UISwipeGestureRecognizer *)swipeGesture
{
    [_navView popTitleAnimated:YES];
    [self popListViewAnimated:YES];
}

- (void)hideSearchBar
{
    if (_navView.searchBarVisible == YES)
    {
        [_navView setSearchBarVisible:NO animated:YES];
        CCascadeListView *listView = [_arListViews lastObject];
        [UIView animateWithDuration:DURATION animations:^{
            [listView setEdgeInsets:UIEdgeInsetsMake(_navView.height, 0, self.data.operateType == ECascadeSelectorOperateTypeSingleSel ? 0 : _completeView.height, 0)];
        } completion:^(BOOL finished) {
            _navView.searchBar.delegate = nil;
            _navView.searchBar.searchfield.text = @"";
            [listView filterWithCondition:nil];
        }];
    }
}

- (void)pushWithData:(CCascadeSelectorCellData *)data animated:(BOOL)animated
{
    [self.dataStack addObject:data];
    
    [_navView pushTitle:data.title animated:animated];
    [self hideSearchBar];
    
    CCascadeListView *listView = [[[CCascadeListView alloc] initWithFrame:_contentView.bounds] autorelease];
    listView.left = _contentView.width;
    [listView setEdgeInsets:UIEdgeInsetsMake(_navView.height, 0, self.data.operateType == ECascadeSelectorOperateTypeSingleSel ? 0 : _completeView.height, 0)];
    listView.delegate = self;
    [listView setCellItems:data.children];
    [self.arListViews addObject:listView];
    [_contentView insertSubview:listView belowSubview:_navView];
    
    CCascadeListView *prevListView = nil;
    if (self.arListViews.count > 1)
    {
        prevListView = [self.arListViews objectAtIndex:self.arListViews.count-2];
    }
    if (animated == NO)
    {
        listView.frame = CGRectOffset(listView.frame, -_contentView.width, 0);
        if (prevListView != nil)
        {
            prevListView.hidden = YES;
            prevListView.frame = CGRectOffset(prevListView.frame, -60, 0);
        }
    }
    else
    {
        [UIView animateWithDuration:DURATION delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            listView.frame = CGRectOffset(listView.frame, -_contentView.width, 0);
            if (prevListView != nil)
            {
                prevListView.frame = CGRectOffset(prevListView.frame, -60, 0);
            }
        } completion:^(BOOL finished) {
            prevListView.hidden = YES;
        }];
    }
}

- (void)popListViewAnimated:(BOOL)animated
{
    if (self.arListViews.count <= 1)
    {
        return;
    }
    [self hideSearchBar];
    
    CCascadeListView *curView = [self.arListViews objectAtIndex:self.arListViews.count-1];
    CCascadeListView *prevView = [self.arListViews objectAtIndex:self.arListViews.count-2];
    prevView.hidden = NO;
    if (animated == NO)
    {
        curView.frame = CGRectOffset(curView.frame, _contentView.width, 0);
        [curView removeFromSuperview];
        [self.arListViews removeObject:curView];
        
        prevView.frame = CGRectOffset(curView.frame, 60, 0);
    }
    else
    {
        [UIView animateWithDuration:DURATION delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            curView.frame = CGRectOffset(curView.frame, _contentView.width, 0);
            prevView.frame = CGRectOffset(prevView.frame, 60, 0);
        } completion:^(BOOL finished) {
            [curView removeFromSuperview];
            [self.arListViews removeObject:curView];
        }];
    }
    [self.dataStack removeLastObject];
}

- (void)completeBtnClecked:(id)sender
{
    [self.delegate cascadeListFrameViewConfirmBtnClicked:self];
}

- (void)setDataVisible:(CCascadeSelectorCellData *)data animated:(BOOL)animated
{
    if ([self.dataStack containsObject:data])
    {
        while ([self.dataStack lastObject] != data && self.dataStack.count > 1)
        {
            [self popListViewAnimated:animated];
        }
    }
    else if (data.parent != nil && [self.dataStack containsObject:data.parent])
    {
        while ([self.dataStack lastObject] != data.parent && self.dataStack.count > 1)
        {
            [self popListViewAnimated:animated];
        }
        CCascadeListView *curView = [self.arListViews lastObject];
        [curView scrollToMakeItemVisible:data animated:animated];
    }
    else
    {
        CCascadeSelectorCellData *parentExist = data.parent;
        NSMutableArray *tmpDataStack = [NSMutableArray array];
        while (parentExist != nil && [self.dataStack containsObject:parentExist] == NO)
        {
            [tmpDataStack addObject:parentExist];
            parentExist = parentExist.parent;
        }
        
        while ([self.dataStack lastObject] != parentExist && self.dataStack.count > 1)
        {
            [self popListViewAnimated:animated];
        }
        while (tmpDataStack.count > 0)
        {
            [self pushWithData:tmpDataStack.lastObject animated:animated];
            [tmpDataStack removeLastObject];
        }
        CCascadeListView *curView = [self.arListViews lastObject];
        [curView scrollToMakeItemVisible:data animated:animated];
    }
}

#pragma mark delegate

- (BOOL)cascadeNavView:(CCascadeNavView *)view willPopupTitleAtIndex:(NSUInteger)index
{
    return YES;
}

- (void)cascadeNavView:(CCascadeNavView *)view didPopupTitleAtIndex:(NSUInteger)index
{
    [self popListViewAnimated:YES];
}

- (void)cascadeNavViewCloseBtnClicked:(CCascadeNavView *)view
{
    [self.delegate cascadeListFrameViewCloseBtnClicked:self];
}

- (void)cascadeListView:(CCascadeListView *)listView didClickCell:(CCascadeListCell *)cell forData:(CCascadeSelectorCellData *)data
{
    if (_navView.isSearchBarVisible == YES)
    {
        [_navView.searchBar.searchfield resignFirstResponder];
    }
    if (data.type == ECascadeSelectorCellDataTypeSet)
    {
        [self pushWithData:data animated:YES];
    }
    else if (data.type == ECascadeSelectorCellDataTypeMultiSelector)
    {
        data.selected = !data.selected;
        [cell updateForType:data.type selected:data.selected animated:YES];
    }
    else if (data.type == ECascadeSelectorCellDataTypeSingleSelector)
    {
        if (_curSelData != nil && _curSelData != data)
        {
            _curSelData.selected = NO;
            if (_curSelData.parent == data.parent)
            {
                CCascadeListCell *cell = [listView cellAtIndexPath:[NSIndexPath indexPathForRow:[_curSelData.parent.children indexOfObject:_curSelData] inSection:0]];
                [cell updateForType:_curSelData.type selected:_curSelData.selected animated:YES];
            }
        }
        data.selected = !data.selected;
        [cell updateForType:data.type selected:data.selected animated:YES];
        if (data.selected == YES)
        {
            _curSelData = data;
        }
        else
        {
            _curSelData = nil;
        }
        [self.delegate cascadeListFrameViewConfirmBtnClicked:self];
    }
}

- (void)cascadeListView:(CCascadeListView *)listView didScrollToOffset:(CGPoint)offset
{
    if (offset.y <= 0 && _navView.searchBarVisible == NO)
    {
        _navView.height = BAR_H+(-offset.y>BAR_H ? BAR_H : -offset.y);
    }
    if (offset.y > 0 &&  _navView.searchBarVisible == NO && _navView.height > BAR_H)
    {
        _navView.height = BAR_H;
    }
}

- (void)cascadeListView:(CCascadeListView *)listView didEndDragAtOffset:(CGPoint)offset
{
    if (offset.y < -BAR_H/2 && _navView.searchBarVisible == NO)
    {
        [_navView setSearchBarVisible:YES animated:NO];
        CCascadeListView *listView = [_arListViews lastObject];
        _navView.searchBar.delegate = self;
        [listView setEdgeInsets:UIEdgeInsetsMake(_navView.height, 0, self.data.operateType == ECascadeSelectorOperateTypeSingleSel ? 0 : _completeView.height, 0)];
    }
}

- (void)searchBar:(XUISearchBar *)searchbar textDidChange:(NSString *)searchText
{
    CCascadeListView *listView = [_arListViews lastObject];
    [listView filterWithCondition:searchText];
}

- (BOOL)searchBarSearchButtonClicked:(XUISearchBar *)searchbar
{
    [searchbar.searchfield resignFirstResponder];
    return NO;
}

@end
